import axios from 'axios'
import type { ApiResponse, UserInfo, PaymentInfo, AuthInfo, ShareInfo, LocationInfo, FunctionItem } from '@/types'

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 可以在这里添加token等认证信息
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('API Error:', error)
    return Promise.reject(error)
  }
)

// 微信相关API
export const wechatApi = {
  // 获取用户信息
  async getUserInfo(): Promise<ApiResponse<UserInfo>> {
    return api.get('/wechat/user/info')
  },

  // 微信授权登录
  async authLogin(code: string): Promise<ApiResponse<AuthInfo>> {
    return api.post('/wechat/auth/login', { code })
  },

  // 一键登录
  async quickLogin(): Promise<ApiResponse<UserInfo>> {
    return api.post('/wechat/login/quick')
  },

  // 创建支付订单
  async createPayment(amount: number, description: string): Promise<ApiResponse<{ orderId: string; paymentUrl: string }>> {
    return api.post('/wechat/payment/create', { amount, description })
  },

  // 查询支付状态
  async getPaymentStatus(orderId: string): Promise<ApiResponse<PaymentInfo>> {
    return api.get(`/wechat/payment/status/${orderId}`)
  },

  // 获取支付历史
  async getPaymentHistory(): Promise<ApiResponse<PaymentInfo[]>> {
    return api.get('/wechat/payment/history')
  },

  // 分享内容
  async shareContent(shareInfo: ShareInfo): Promise<ApiResponse<{ shareId: string }>> {
    return api.post('/wechat/share', shareInfo)
  },

  // 生成二维码
  async generateQRCode(content: string, size: number = 200): Promise<ApiResponse<{ qrCodeUrl: string }>> {
    return api.post('/wechat/qrcode/generate', { content, size })
  },

  // 获取位置信息
  async getLocation(): Promise<ApiResponse<LocationInfo>> {
    return api.get('/wechat/location')
  },

  // 更新位置信息
  async updateLocation(location: LocationInfo): Promise<ApiResponse<void>> {
    return api.post('/wechat/location', location)
  },

  // 获取功能列表
  async getFunctionList(): Promise<ApiResponse<FunctionItem[]>> {
    return api.get('/function/list')
  },

  // 根据ID获取功能详情
  async getFunctionById(id: string): Promise<ApiResponse<FunctionItem>> {
    return api.get(`/function/${id}`)
  }
}

export default api

